<template>
   <div class="scan">
    <div id="bcid">
      <div style="90%"></div>
    </div>
  </div>
</template>
<script>
 let scan = null;
export default {
 mounted() {
   this.startRecognize()
   this.startScan()
 },
 methods: {
      //创建扫描控件
      startRecognize() {
        let that = this;
        if (!window.plus) return;
        scan = new plus.barcode.Barcode('bcid');
       scan.setStyle({background:"green"})
 
      var onmarked = (type, result, file)=>{
          switch (type) {
            case plus.barcode.QR:
              type = 'QR';
              break;
            case plus.barcode.EAN13:
              type = 'EAN13';
              break;
            case plus.barcode.EAN8:
              type = 'EAN8';
              break;
            default:
              type = '其它' + type;
              break;
          }
          result = result.replace(/\n/g, '');
         
          console.log(result)//这里的result就是扫码获取到的值    
          
          that.closeScan();
        }
         scan.onmarked = onmarked;
      },
      //开始扫描
      startScan() {
        if (!window.plus) return;
        scan.start();
				console.log('开始扫描=====')
      },
      //关闭扫描
      cancelScan() {
        if (!window.plus) return;
        scan.cancel();
      },
      //关闭条码识别控件
      closeScan() {
        if (!window.plus) return;
        scan.close();
      }
 },
 
}
</script>